from rest_framework.generics import GenericAPIView, ListAPIView, CreateAPIView
from rest_framework.mixins import ListModelMixin
from rest_framework.response import Response
from rest_framework.views import APIView
from rest_framework.viewsets import ModelViewSet
from apps.goods.models import SKU, GoodsCategory, SPU
from apps.meiduo_admin.serializers.skus import SKUSerializer, SKUSimpleCategorySerializer, SPUSimpleListSerializer, SKUSimpleListSerializer
from apps.meiduo_admin.utils import PageNum


class SKUSModelViewSet(ModelViewSet):

    serializer_class = SKUSerializer
    pagination_class = PageNum

    def get_queryset(self):

        keyword = self.request.query_params.get('keyword')
        if keyword is None or keyword == '':
            queryset = SKU.objects.all()
        else:
            queryset = SKU.objects.filter(name__contains=keyword)
        return queryset


# class SKUSimpleCategoryAPIView(APIView):
#
#     def get(self, request):
#
#         queryset = GoodsCategory.objects.filter(subs=None)
#         s = SKUSerializer(queryset, many=True)
#         return Response(s.data)

# class SKUSimpleCategoryGenericAPIView(ListModelMixin, GenericAPIView):
#
#     queryset = GoodsCategory.objects.filter(subs=None)
#     serializer_class = SKUSerializer
#
#     def get(self, request):
#         return self.list(request)

class SKUSimpleCategoryListAPIView(ListAPIView):

    queryset = GoodsCategory.objects.filter(subs=None)
    serializer_class = SKUSimpleCategorySerializer


class SPUSimpleListAPIView(ListAPIView):

    queryset = SPU.objects.all()
    serializer_class = SPUSimpleListSerializer


class SKUSimpleListView(ListAPIView):

    """给sku商品添加图片时，先获取sku简单列表"""

    queryset = SKU.objects.all()
    serializer_class = SKUSimpleListSerializer


